package com.maimang.platform.quartz.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname ScheduleConfig
 * @description
 * @date 2019-03-30 13:41
 * @email jintaozhao@qq.com
 */
@Configuration
public class ScheduleConfig {

	private String schedulerName = "platformScheduler";

	@Value("${spring.datasource.driver-class-name}")
	private String driver;
	@Value("${spring.datasource.druid.master.url}")
	private String url;
	@Value("${spring.datasource.druid.master.username}")
	private String username;
	@Value("${spring.datasource.druid.master.password}")
	private String password;
	@Value("${spring.datasource.druid.maxActive}")
	private String maxConnections;


	public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource) {
		SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
		factoryBean.setDataSource(dataSource);
		Properties prop = createProperties();
		factoryBean.setQuartzProperties(prop);
		factoryBean.setSchedulerName(schedulerName);
		// 延时启动
		factoryBean.setStartupDelay(1);
		factoryBean.setApplicationContextSchedulerContextKey("applicationContextKey");
		// 设置自动启动，默认为true
		factoryBean.setAutoStartup(true);
		// 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
		factoryBean.setOverwriteExistingJobs(true);
		return factoryBean;
	}

	private Properties createProperties() {
		// 设置quartz参数
		Properties prop = new Properties();
		prop.put("org.quartz.scheduler.instanceName", schedulerName);
		prop.put("org.quartz.scheduler.instanceId", "AUTO");
		// 线程池配置
		prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
		prop.put("org.quartz.threadPool.threadCount", "20");
		prop.put("org.quartz.threadPool.threadPriority", "5");
		prop.put("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
		// JobStore配置
		prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
		prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
		// 集群配置
		prop.put("org.quartz.jobStore.isClustered", "true");
		prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
		prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
		prop.put("org.quartz.jobStore.txIsolationLevelSerializable", "true");

		// 数据库配置
		prop.put("org.quartz.jobStore.dataSource", "myDS");
		prop.put("org.quartz.dataSource.myDS.driver", driver);
		prop.put("org.quartz.dataSource.myDS.URL", url);
		prop.put("org.quartz.dataSource.myDS.user", username);
		prop.put("org.quartz.dataSource.myDS.password", password);
		prop.put("org.quartz.dataSource.myDS.maxConnections", maxConnections);
		prop.put("org.quartz.dataSource.myDS.validationQuery", "select count(0) from");
		// sqlserver 启用
		// prop.put("org.quartz.jobStore.selectWithLockSQL", "SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME = ?");
		prop.put("org.quartz.jobStore.misfireThreshold", "12000");
		prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");

		return prop;
	}

}
